package com.ljx.recruitment.sale.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.recruitment.common.domain.vo.resp.PageResp;
import com.ljx.recruitment.common.enums.CommonStatusTypeEnum;
import com.ljx.recruitment.common.exception.ApiException;
import com.ljx.recruitment.sale.domain.entity.TrialApplication;
import com.ljx.recruitment.sale.domain.vo.req.ProductTrialApplicationPageReq;
import com.ljx.recruitment.sale.domain.vo.req.ProductTrialApplicationSubmitReq;
import com.ljx.recruitment.sale.domain.vo.req.SmsVerifyReq;
import com.ljx.recruitment.sale.enums.TrialApplicationProcessStatusEnum;
import com.ljx.recruitment.sale.mapper.TrialApplicationMapper;
import com.ljx.recruitment.sale.service.ProductService;
import com.ljx.recruitment.sale.service.SmsService;
import com.ljx.recruitment.security.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private TrialApplicationMapper trialApplicationMapper;

    @Autowired
    private SmsService smsService;

    @Override
    public void submitTrialApplication(ProductTrialApplicationSubmitReq req) {
        checkTrialApplicationSubmitParams(req);
        updateOrSaveTrialApplication(req);
    }

    private void updateOrSaveTrialApplication(ProductTrialApplicationSubmitReq req) {
        TrialApplication trialApplication;
        if (Objects.isNull(req.getId())) {
            trialApplication = BeanUtil.copyProperties(req, TrialApplication.class);
            trialApplication.setProcessStatus(TrialApplicationProcessStatusEnum.WAIT_DONE.getKey());
            trialApplicationMapper.insert(trialApplication);
        } else {
            trialApplication = trialApplicationMapper.selectById(req.getId());
            BeanUtils.copyProperties(req, trialApplication);
            if (UserUtil.checkIsAnonymous()) {
                trialApplication.setProcessStatus(TrialApplicationProcessStatusEnum.DONE.getKey());
            }
            trialApplicationMapper.updateById(trialApplication);
        }
    }

    private void checkTrialApplicationSubmitParams(ProductTrialApplicationSubmitReq req) {
        if (StrUtil.isEmpty(req.getName())) {
            throw new ApiException("姓名不能为空");
        }
        if (StrUtil.isEmpty(req.getPhone())) {
            throw new ApiException("电话不能为空");
        }
        if (StrUtil.isEmpty(req.getCompany())) {
            throw new ApiException("公司不能为空");
        }
        if (!smsService.verify(new SmsVerifyReq(req.getType(), req.getKey(), req.getCode()))) {
            throw new ApiException("验证码错误");
        }
        TrialApplication trialApplication = trialApplicationMapper.getByCompany(req.getCompany());
        if (!UserUtil.checkIsAnonymous() && Objects.nonNull(trialApplication)) {
            throw new ApiException("该公司已经提交，请勿重复提交");
        }
        if (Objects.nonNull(trialApplication) &&
                (TrialApplicationProcessStatusEnum.REJECT.getKey().equals(trialApplication.getProcessStatus()) ||
            CommonStatusTypeEnum.BAN.getKey().equals(trialApplication.getStatus()))) {
            throw new ApiException("该申请表已被取消");
        }
        if (Objects.nonNull(trialApplication) &&
                TrialApplicationProcessStatusEnum.DONE.getKey().equals(trialApplication.getProcessStatus())) {
            throw new ApiException("该申请表已被处理");
        }
    }

    @Override
    public void adminSubmitTrialApplication(ProductTrialApplicationSubmitReq req) {
        if (Objects.isNull(req.getId())) {
            throw new ApiException("ID不能为空");
        }
        if (Objects.isNull(req.getProcessStatus())) {
            throw new ApiException("状态不能为空");
        }
        updateOrSaveTrialApplication(req);
    }

    @Override
    public PageResp<TrialApplication> getTrialApplicationPage(ProductTrialApplicationPageReq req) {
        LambdaQueryWrapper<TrialApplication> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(req.getKey())){
            lambdaQueryWrapper.like(TrialApplication::getPhone, req.getKey());
        }
        Page<TrialApplication> page = trialApplicationMapper.selectPage(new Page<>(req.getCurrPage(), req.getPageSize()), lambdaQueryWrapper);

        return PageResp.<TrialApplication>builder()
                .list(page.getRecords())
                .total(page.getTotal())
                .build();
    }

    @Override
    public TrialApplication getTrialApplication(Long trialApplicationId) {
        if (Objects.isNull(trialApplicationId)) {
            throw new ApiException("id不能为空");
        }
        TrialApplication trialApplication = trialApplicationMapper.selectById(trialApplicationId);
        if (CommonStatusTypeEnum.BAN.getKey().equals(trialApplication.getStatus())) {
            throw new ApiException("表单已被禁用");
        }
        return trialApplication;
    }
}
